using System.Data ;
using System.Net ;
using System.IO ;
using System ;
using System.Text ;
using System.Globalization ;
using System.Text.RegularExpressions ;
using System.Web;
using System.Xml;

namespace Is.Net.TVSchedule {

	public class TVStations  {

		private string GetUrlContent(string url) {
			return GetUrlContent(url, System.Text.Encoding.GetEncoding(1252));
		}
		private string GetUrlContent(string url, Encoding enc) {
			HttpWebResponse loWebResponse = null;
			StreamReader loResponseStream = null;
			string lcHtml = "";

			try {
				HttpWebRequest loHttp = (HttpWebRequest) WebRequest.Create(url);
				loWebResponse = (HttpWebResponse) loHttp.GetResponse();

				loResponseStream = new StreamReader(loWebResponse.GetResponseStream(),enc);
				lcHtml = loResponseStream.ReadToEnd();
			} catch (Exception ex) {
				throw ex;
			} finally {
				if (loWebResponse != null) loWebResponse.Close();
				if (loResponseStream != null) loResponseStream.Close();	
			}

			return lcHtml;
		}

		
		public Station[] GetStations() {
			Station[] s = new Station[13];
			s[0] = new Station("ruv", "Rúv");
			s[1] = new Station("stod2", "Stöð 2");
			s[2] = new Station("s1", "Skjár 1");
			s[3] = new Station("stod2extra", "Stöð 2 Extra");
			s[4] = new Station("stod2bio", "Stöð 2 Bíó");
			s[5] = new Station("stod2sport", "Stöð 2 Sport");
			s[6] = new Station("stod2sport2", "Stöð 2 Sport 2");
			s[7] = new Station("stod2sport3", "Stöð 2 Sport 3");
			s[8] = new Station("stod2sport4", "Stöð 2 Sport 4");
			s[9] = new Station("stod2sport5", "Stöð 2 Sport 5");
			s[10] = new Station("stod2sport6", "Stöð 2 Sport 6");
			
			s[11] = new Station("ras1", "Rás 1");
			s[12] = new Station("ras2", "Rás 1");
			return s;
		}


		
		public Entry[] GetTodayById(string id) {
			return GetById(id, 0);
		}

		
		public Entry[] GetById(string id, int addDay) {
			switch (id) {
				case "s1":
					return Skjar1(addDay);
				case "stod2":
					return Stod2(addDay);
				case "ras1":
					return Ras1(addDay);
				case "ras2":
					return Ras2(addDay);
				case "stod2extra":
					return Stod2Extra(addDay);
				case "stod2bio":
					return Stod2Bio(addDay);
				case "stod2sport":
					return Stod2Sport(addDay);
				case "stod2sport2":
					return Stod2Sport2(addDay);
				case "stod2sport3":
					return Stod2Sport3(addDay);
				case "stod2sport4":
					return Stod2Sport4(addDay);
				case "stod2sport5":
					return Stod2Sport5(addDay);
				case "stod2sport6":
					return Stod2Sport6(addDay);
				default:
					return Ruv(addDay);

			}
		}


		
		public Entry[] Stod2(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=228", "Stod2");
		} 
		
		public Entry[] Stod2Extra(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=232", "Stod2Extra");
		} 
		
		public Entry[] Stod2Bio(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=233", "Stod2Bio");
		} 
		
		public Entry[] Stod2Sport(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=230", "Stod2Sport");
		} 

		
		public Entry[] Stod2Sport2(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=231", "Stod2Sport2");
		} 

		
		public Entry[] Stod2Sport3(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=235", "Stod2Sport3");
		} 

		
		public Entry[] Stod2Sport4(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=236", "Stod2Sport4");
		} 

		
		public Entry[] Stod2Sport5(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=237", "Stod2Sport5");
		} 

		
		public Entry[] Stod2Sport6(int addDay) {
			return Stod2Xml(addDay, "http://www.stod2.is/?pageid=238", "Stod2Sport6");
		} 


		private Entry[] Stod2Xml(int addDay, string url, string channel) {
			string content = "";
			if (HttpContext.Current != null) {
				if (HttpContext.Current.Cache["Channels_" + channel + "_" + addDay] == null) {
					content = GetUrlContent(url, System.Text.Encoding.UTF8);
					HttpContext.Current.Cache.Insert("Channels_" + channel + "_" + addDay, content, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
				} else {
					content = (string)HttpContext.Current.Cache["Channels_" + channel + "_" + addDay];
				}
			} else {
				content = GetUrlContent(url, System.Text.Encoding.UTF8);
			}
			XmlDocument doc = new XmlDocument();
			doc.LoadXml(content);
				
			XmlNodeList events = doc.GetElementsByTagName("event");
			Entry[] entries = new Entry[events.Count];

			XmlNode eve;
			for (int i=0;i<events.Count;i++) { 
				eve = events[i];
				entries[i] = new Entry();
				entries[i].Title			= eve["title"].InnerText;
				entries[i].Time				= Convert.ToDateTime (eve.Attributes["starttime"].InnerText);
				entries[i].Description		= eve["description"].InnerText;
			}

			return entries;
		}


		
		public Entry[] Skjar1(int addDays) {

	//http://www.skjarinn.is/other_files/skjarinn/fla/content/xml/skjarinn_tvschedule.xml
			Entry[] entries;
			string content;
			if (HttpContext.Current.Cache["Channels_Skjar1_" + addDays] == null) {
				content = GetUrlContent("http://vefur.skjarinn.is/WebData/xml/Dagskra/S1-XmlSchedule.xml", System.Text.Encoding.Default);
				HttpContext.Current.Cache.Insert("Channels_Skjar1_" + addDays, content, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
			} else {
				content = (string) HttpContext.Current.Cache["Channels_Skjar1_" + addDays];
			}
		
			XmlDocument doc = new XmlDocument();
			doc.XmlResolver = null;
			doc.LoadXml(content);

			XmlNodeList events = doc.GetElementsByTagName("event");
			entries = new Entry[events.Count];
			for (int i=0;i<events.Count;i++) {
				XmlNode eve = events[i];
				entries[i] = new Entry();
				entries[i].Time = Convert.ToDateTime(eve.Attributes["start-time"].Value);
				entries[i].Title = eve["short-event-descriptor"]["event-name"].InnerText;
				entries[i].Description = eve["short-event-descriptor"]["short-description"].InnerText;
			}

			return entries;
		}

		
		public Entry[] Ras1(int addDays) {
			return RuvRas(addDays, 2);
		}
		
		public Entry[] Ras2(int addDays) {
			return RuvRas(addDays, 3);
		}
		
		public Entry[] Ruv(int addDays) {
			return RuvRas(addDays, 1);
		}
		public Entry[] RuvRas(int addDays, int channel) {
			Entry[] entries;
			string content;

			if (HttpContext.Current.Cache["Channels_Ruv_" + addDays + "_" + channel] == null) {
				content = GetUrlContent("http://dagskra.ruv.is/files/xml.aspx?channel=" + channel + "&date-from=" + DateTime.Now.ToString("yyyy-MM-dd") + "&date-to=" + DateTime.Now.AddDays(addDays).ToString("yyyy-MM-dd"), Encoding.UTF8);
				HttpContext.Current.Cache.Insert("Channels_Ruv_" + addDays + "_" + channel, content, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
			} else {
				content = ((string) HttpContext.Current.Cache["Channels_Ruv_" + addDays + "_" + channel]);
			}

			XmlDocument doc = new XmlDocument();
			doc.LoadXml(content);
			
			XmlNodeList events = doc.GetElementsByTagName("event");
			entries = new Entry[events.Count];

			XmlNode node;
			for (int i=0;i<events.Count;i++) { 
				node = events[i];
				entries[i] = new Entry();
				entries[i].Time				= Convert.ToDateTime (events[i].Attributes["start-time"].Value);
				entries[i].Title			= events[i]["title"].InnerText ;
				entries[i].Description		= events[i]["description"].InnerText ;
				entries[i].Duration			= events[i].Attributes["duration"].Value ;
			}

			return entries;

		}

		


	}

	
}